package com.fare.taluo.core.domain.thread;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import net.htmlparser.jericho.Element;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.conf.PropertyUtils;
import com.fare.taluo.core.domain.config.city.PortEntity;
import com.fare.taluo.core.domain.config.line.FeiChangZhunFlightInfoEntity;
import com.fare.taluo.core.domain.config.line.FlightInfoEntity;
import com.fare.taluo.core.domain.crawl.CrawlCSAirLineModel;
import com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader;
import com.fare.taluo.core.domain.crawl.adapter.sub.CEAirLineAdaptor;
import com.fare.taluo.core.domain.crawl.adapter.sub.FeiChangZhunAdapter;
import com.fare.taluo.core.domain.entity.pretreatment.QueueLineEntity;
import com.foreveross.proxyip.ProxyIpModel;
import com.foreveross.proxyip.ProxyIpProviderFactory;

public class FeiChangZhunLineThread extends Thread {
	private List<String> cityNames;
	private CountDownLatch threadsSignal;

	public FeiChangZhunLineThread(List<String> cityNames,
			CountDownLatch threadsSignal) {
		this.cityNames = cityNames;
		this.threadsSignal = threadsSignal;
	}

	@Override
	public void run() {

		ConfigContainer.getInstance().register(
				"com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
		PortEntity portEntity = new PortEntity();
		// 循环去访问网站尝试
		for (Iterator<String> iterator = cityNames.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			String[] namesAndCodes = string.split("&");
			String fromCity = namesAndCodes[0].split("-")[0];
			String toCity = namesAndCodes[0].split("-")[1];
			String fromCityCode = namesAndCodes[1].split("-")[0];
			String toCityCode = namesAndCodes[1].split("-")[1];
			//广州 上海浦东
			/*String fromCity = "广州";
			String toCity = "上海浦东";
			String fromCityCode = "PEK";
			String toCityCode = "CAN";*/
			//System.out.println(fromCity+" "+ toCity);
			// 抓取机场模型
			CrawlCSAirLineModel portModel = new CrawlCSAirLineModel();
			portModel.setFromCity(fromCity);
			portModel.setToCity(toCity);
			portModel.setFromCityCode(fromCityCode);
			portModel.setToCityCode(toCityCode);
			FeiChangZhunAdapter adap = new FeiChangZhunAdapter(portModel);
			Map<String,Element>  obj = null;
			int fetchCount = PropertyUtils.getIntProperty(
					"crawl.line.fail.repeat.times",
					CrawlPropertiesLoader.CONFIG_FILE);
			try {
				while (fetchCount > 0) {
					obj = (Map<String, Element>) adap.fetch(null);
					fetchCount--;
					if (null != obj) {
						List<Object> results = adap.paraseToVo(obj);
						if (null == results) {
							System.out.println(fromCity + "-" + toCity);
							break;
						}
						// 循环保存航班信息
						if (null != results && results.size() > 0) {
							for (Iterator iterator2 = results.iterator(); iterator2
									.hasNext();) {
								FeiChangZhunFlightInfoEntity entity = (FeiChangZhunFlightInfoEntity) iterator2
										.next();
								entity.tranSave(entity);//---------------------------
								System.out.println("save :" + entity.getFromCityName() +" "+ entity.getToCityName());
								/*if (null != map.get(entity.getFromCity())
										&& null != map.get(entity.getToCity())) {
									flightEntity.tranSave(entity);
								}*/

							}
						}
					}

				}

			} catch (Exception e1) {
				e1.printStackTrace();
			}

			QueueLineEntity.feiCount();

		}
		System.out.println("---------finished---------");
		threadsSignal.countDown();
	}
}
